/**
 * 商品分类
 * */


var pages = {
    start: 1, limit: pageSize,
    url: '/admin/gshop.html'
};
pages.start = getParamByUrl('start');
pages.start = pages.start ? pages.start : 1;


(function () {
    getList();
})();


//分类列表
function getList(){
    $.ajax({
        type: apis.shopIndex.method,
        url: apis.shopIndex.url,
        data: { start: pages.start, limit: pages.limit },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            $('#pageTotal').html(d.length);
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                h += '<tr class="text-c">';
                h += '<td>' + d[i].shop_name + '</td>';
                h += '<td>' + d[i].address + '</td>';
                h += '<td>' + d[i].phone + '</td>';
                h += '<td>' + d[i].uptime + '</td>';
                h += '<td class="f-14">';
                h += '<a href="javascript:;" onclick="gshop_edit(\'编辑\',\'gshop-edit.html\', ' + d[i].id + ');" style="text-decoration:none">';
                h += '编辑 <i class="Hui-iconfont">&#xe6df;</i>';
                h += '</a>';
                h += ' &nbsp; ';
                h += '<a href="javascript:;" onclick="gshop_del(' + d[i].id + ');" style="text-decoration: none; color: red;">';
                h += '删除 <i class="Hui-iconfont">&#xe6e2;</i>';
                h += '</a>';
                h += '</td>';
                h += '</tr>';
            }
            $('tbody').html(h);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//获取一个
function getOne(v){
    $.ajax({
        type: apis.shopOne.method,
        url: apis.shopOne.url,
        data: { id: v },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            $('#shop_name')[0].value = res.data.shop_name;
            $('#address')[0].value = res.data.address;
            $('#phone')[0].value = res.data.phone;
            $('#map').html(res.data.map);
            var map = res.data.map, lat, lng;
            map = map=='' ? ['',''] : map.split(',');
            initMap({wd: map[0], jd: map[1]});
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


//编辑
function gshop_edit(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,650);
}


//删除
function gshop_del(id){
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg,function(index){
        //$(obj).parents("tr").remove();
        //console.log(id, index);
        $.ajax({
            type: apis.shopDel.method,
            url: apis.shopDel.url,
            data: { id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg}); return
                }
                layer.msg('已删除!',{icon: 1,time: 1000});
                setTimeout(function(){
                    top.location.reload();
                }, 1000);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}


//保存更新
function save(){
    var formData = {
        id: id,
        shop_name: $('#shop_name').val(),
        address: $('#address').val(),
        phone: $('#phone').val(),
        map: $('#map').html(),
    };
    if (!formData.shop_name) {
        msgPop({msg: '门店名称必须的'}); return
    }
    if (!formData.address) {
        msgPop({msg: '门店地址必须的'}); return
    }
    if (!formData.map) {
        msgPop({msg: '门店坐标必须的'}); return
    }
    //console.log(formData); return
    $.ajax({
        type: apis.shopModify.method,
        url: apis.shopModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var operMsg = id ? '修改' : '添加';
            msgPop({msg: operMsg + '成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}

//拾取坐标
var map, center, position, lat, lng;
function initMap(e) {
    position = document.getElementById('map');
    console.log(e);
    lat = e.wd == '' ? '39.984104' : e.wd;
    lng = e.jd == '' ? '116.307503' : e.jd;
    center = new TMap.LatLng(lat, lng);//设置中心点坐标
    //初始化地图
    map = new TMap.Map('container', {
        center: center,
    });
    //绑定点击事件
    map.on("click", function (evt) {
        lat = evt.latLng.getLat().toFixed(6);
        lng = evt.latLng.getLng().toFixed(6);
        position.innerHTML = lat + "," + lng;
    });
}

//地址转经纬度
function addressToMap(){
    var seakey = $('#seakey').val();
    position = document.getElementById('map');
    $.ajax({
        type: apis.shopAddressToMap.method,
        url: apis.shopAddressToMap.url,
        data: { address: seakey },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#container').html('');
            var lat = res.data.location.lat;
            var lng = res.data.location.lng;
            center = new TMap.LatLng(lat, lng);
            map = new TMap.Map('container', {
                center: center
            });
            position.innerHTML = lat + "," + lng;
            //绑定点击事件
            map.on("click", function (evt) {
                lat = evt.latLng.getLat().toFixed(6);
                lng = evt.latLng.getLng().toFixed(6);
                position.innerHTML = lat + "," + lng;
            });
        },
        error: function () {
            console.log('错误辣');
        }
    });
}